//
//  SYLJWelcomeViewController.swift
//  SYLJWeibo
//
//  Created by 刘俊 on 15/10/19.
//  Copyright © 2015年 刘俊. All rights reserved.
//

import UIKit

class SYLJWelcomeViewController: UIViewController {

    // MARK: - 动画处理
    // 一般动画和事件处理在视图显示之后处理
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        welLable.hidden = true
        UIView.animateWithDuration(1.0,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 5,
            options: [],
            animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
                self.welLable.hidden = false
                }, completion: { (_) -> Void in
                  // 发送通知,跳转控制器
                    NSNotificationCenter.defaultCenter().postNotificationName(SYLJSwitchRootViewControllerNotification, object: nil)
            })
        }
    }
    

    // MARK: - 懒加载控件
    /// 背景图像
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
    /// 默认头像
    private lazy var iconView: UIImageView = {
        let icon = UIImageView(imageName: "avatar_default_big")
        icon.layer.cornerRadius = 45
        icon.layer.masksToBounds = true
        return icon
    }()
    /// 欢迎标签
    private lazy var welLable: UILabel = UILabel(title: "欢迎归来", fontSize: 17)
    
    // MARK: - 搭建界面
    override func loadView() {
        view = backImageView

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
}

// MARK: - 设置界面
extension SYLJWelcomeViewController {
    private func setupUI() {
        // 添加控件
        view.addSubview(iconView)
        view.addSubview(welLable)
        
        // 设置frame
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        welLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
}
